<template>
    <div>
        <h1>病房显示</h1>
        <div>
            病房名称 : <input type="text" v-model="queryinfo.wardName">
            <input type="button" value="查询" @click="show">
        </div>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td>病房id</td>
                    <td>所属科室</td>
                    <td>床位数</td>
                    <td>病房名称</td>
                    <td>状态</td>
                    <td>空床位</td>
                    <td>创建时间</td>
                    <td>更新时间</td>
                    <td>操作</td>
                </tr>
                <tr v-for="item in data.info">
                    <td>{{ item.wardId }}</td>
                    <td>{{ item.departmentName }}</td>
                    <td>{{ item.bedNumber }}</td>
                    <td>{{ item.wardName }}</td>
                    <td>{{ item.state ? "启用" : "维修" }}</td>
                    <td>{{ item.emptyBedNumber }}</td>
                    <td>{{ moment(item.createTime).format("YYYY-MM-DD") }}</td>
                    <td>{{ moment(item.updateTime).format("YYYY-MM-DD") }}</td>
                    <td><input type="button" value="修改状态" @click="Updatestate(item.wardId)"></td>
                </tr>
            </tbody>
        </table>
        <div>
            <span>共{{ data.totalcount }}条</span>
            <span>当前{{ queryinfo.pageindex }}页/共{{ data.totalpage }}页</span>
            <input type="button" value="首页" @click="goPage(1)">
            <input type="button" value="上一页" @click="goPage(queryinfo.pageindex - 1)">
            <span v-for="item in data.totalpage">{{ item }}</span>
            <input type="button" value="下一页" @click="goPage(queryinfo.pageindex + 1)">
            <input type="button" value="尾页" @click="goPage(data.totalpage)">

        </div>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, onMounted } from 'vue';
import moment from 'moment';
import { useRoute, useRouter } from 'vue-router';
const route = useRoute();
const router = useRouter();

onMounted(() => {
    getwardlist();
})

let queryinfo: any = reactive({
    wardName: "",
    pageindex: 1,
    pagesize: 3
})
let data: any = reactive({
    totalcount: 0,
    totalpage: 0,
    info: []
})

const getwardlist = () => {
    axios({
        url: '/api/Ward/GetWardInformationList',
        method: 'get',
        params: queryinfo,
    })
        .then(res => {
            Object.assign(data, res.data);
        })
        .catch(err => {

        })
}

const goPage = (num: any) => {
    if (num < 1) {
        queryinfo.pageindex = 1;
        return;
    }
    if (num > data.totalpage) {
        queryinfo.pageindex = data.totalpage;
        return;
    }
    queryinfo.pageindex = num;
    getwardlist();
}

const show = () => {
    queryinfo.pageindex = 1;
    getwardlist();
}

const Updatestate = (id: any) => {
    if (confirm("你确定要修改吗")) {
        axios({
            url: '/api/Ward/WardUpdateState',
            method: 'put',
            params: { id: id }
        })
            .then(res => {
                if (res.data > 0) {
                    alert("修改状态成功");
                    getwardlist();
                    return;
                }
                else {
                    alert("修改失败");
                    return;
                }
            })
            .catch(err => {

            })
    }

}
</script>

<style scoped></style>